//
//  YTRankViewController.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/8/12.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTRankViewController.h"
#import "YTRankButton.h"
#import "YTRankHeaderView.h"
#import "YTRankCell.h"
#import "YTRankModel.h"

#define kRankHeaderViewH 98
#define kRankRowHeight 49
NSString *rankCellID = @"rankCellID";
@interface YTRankViewController ()<UITableViewDataSource>
@property(nonatomic ,strong)YTRankHeaderView *headerView;

@property(nonatomic ,strong)NSArray<YTRankModel *> *rankList;

@property(nonatomic ,strong)UITableView *tableView;

@property(nonatomic ,strong)NSString *type;
@end

@implementation YTRankViewController

// lazy tableView
- (UITableView *)tableView
{
    if (!_tableView) {
        
        NSLog(@"%f",CGRectGetMaxY(self.headerView.frame));
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.headerView.frame), kScreenWidth, kScreenHeight - kRankHeaderViewH - kRankRowHeight)];
        [_tableView registerNib:[UINib nibWithNibName:@"YTRankCell" bundle:nil] forCellReuseIdentifier:rankCellID];
        _tableView.dataSource = self;
        _tableView.rowHeight = kRankRowHeight;
    }
    return _tableView;
}

// lazy headerView
- (YTRankHeaderView *)headerView
{
    if (!_headerView) {
        _headerView = [YTRankHeaderView headerViewWithFrame:CGRectMake(0, 0, kScreenWidth, kRankHeaderViewH)];
        
        __weak typeof(self) WeakSelf = self;
        _headerView.buttonClickBlock = ^(NSString *type) {
            WeakSelf.type = type;
            
            [WeakSelf loadData];
        };
    }
    return _headerView;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // setup navbar
    self.navigationItem.title = @"排行榜";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage dh_setNavImageName:@"倛袨1"] style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClick)];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : UIColor.blackColor}];
    self.view.backgroundColor = UIColor.whiteColor;
    
    // setup headerview
    [self.view addSubview:self.headerView];
    
    // setup table view
    [self.view addSubview:self.tableView];
    
    [self loadData];
    
    
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : UIColor.whiteColor}];

}


#pragma mark: - 加载数据
- (void)loadData
{
    [YTNetworkManager.shareManager requestWithRankListWithTypes:self.type ? self.type : @"1"
                                                      page_size:@"50"
                                                     completion:^(NSArray<YTRankModel *> *rankList, BOOL isSucc, NSString *error)
    {
        if (!isSucc) {
            [self.view showErrorWithTipStr:error];
            return ;
        }
        
        self.rankList = rankList;
        
        [self.tableView reloadData];
    }];
}

// 返回按钮
- (void)backButtonClick
{
    [self.navigationController popViewControllerAnimated:true];
}

#pragma mark: - table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.rankList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YTRankCell *cell = [tableView dequeueReusableCellWithIdentifier:rankCellID forIndexPath:indexPath];
    cell.model = self.rankList[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.tag = indexPath.row + 1;
    return cell;
}

@end
